//
//  WelcomeView.swift
//  ShangHaiWeiBo
//
//  Created by 蔡杰 on 16/2/28.
//  Copyright © 2016年 蔡杰. All rights reserved.
//

import UIKit

class WelcomeView: UIView {

 
    override init(frame: CGRect) {
        super.init(frame: UIScreen.mainScreen().bounds)
        setWelcomeUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func didMoveToWindow() {
        
        avatarView.snp_updateConstraints { (make) -> Void in
            make.centerY.equalTo(self).offset(-100)
        }
        //动画
        UIView.animateWithDuration(1.2, delay: 0.0, usingSpringWithDamping: 0.8, initialSpringVelocity: 10, options: [], animations: { () -> Void in
            self.layoutIfNeeded()
            }) { (_) -> Void in
                self.removeFromSuperview()
        }
    }
    override func layoutSubviews() {
        super.layoutSubviews()
        
        print("\(__FUNCTION__)")
    }
    private lazy var avatarView = UIImageView(cj_image: "avatar_default_big")
    private lazy var welcomeLabel = UILabel(cj_text: "欢迎回来", fontSize: 18)

}

extension WelcomeView {

    private func setWelcomeUI (){
    
        backgroundColor = UIColor(patternImage: UIImage(named: "ad_background")!)
        
        addSubview(avatarView)
        addSubview(welcomeLabel)
        
        let iconWH: CGFloat = 45
        avatarView.layer.cornerRadius = iconWH
        avatarView.layer.masksToBounds = true
        
        avatarView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(self)
            make.centerY.equalTo(self)
            make.size.equalTo(CGSize(width: iconWH * 2, height: iconWH * 2))
        }
        welcomeLabel.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(avatarView)
            make.top.equalTo(avatarView.snp_bottom).offset(20)
            
        }
        
         layoutIfNeeded()
        
    }


}